unit Urelatorio;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Buttons, StdCtrls, Grids, DB, ZAbstractRODataset,
  ZAbstractDataset, ZDataset;

type
  TfrmRelatorio = class(TForm)
    Panel1: TPanel;
    resultados: TStringGrid;
    bt_anterior: TButton;
    bt_proxima: TButton;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure bt_proximaClick(Sender: TObject);
    procedure resultadosDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure bt_anteriorClick(Sender: TObject);
  private
    { Private declarations }
  public
    sql:tzquery;
    procedure carrega_relatorio;
  end;

var
  frmRelatorio: TfrmRelatorio;

implementation

uses Udm, Uprincipal;

{$R *.dfm}

procedure AutoSizeGrid(Grid: TStringGrid);
const
  ColWidthMin = 10;
var
  C, R, W, ColWidthMax: integer;
begin
  for C := 0 to Grid.ColCount - 1 do begin
    ColWidthMax := ColWidthMin;
    for R := 0 to (Grid.RowCount - 1) do begin
      W := Grid.Canvas.TextWidth(Grid.Cells[C, R]);
      if W > ColWidthMax then
        ColWidthMax := W;
    end;
    Grid.ColWidths[C] := ColWidthMax + 5;
  end;
end;

procedure TfrmRelatorio.FormShow(Sender: TObject);
begin
 carrega_relatorio;
 SendMessage(resultados.Handle, WM_VSCROLL, SB_TOP, 0);
 bringwindowtotop(self.Handle);
 self.WindowState := wsNormal;

end;

procedure TfrmRelatorio.FormCreate(Sender: TObject);
begin
 sql := tzquery.create(Application);
 sql.Connection := dm.conexao;
end;

procedure TfrmRelatorio.FormDestroy(Sender: TObject);
begin
 sql.Free;
end;

procedure TfrmRelatorio.carrega_relatorio;
var i,i2:word;
begin
 if pos('LIMIT',sql.SQL.Text) = 0 then sql.SQL.Add('LIMIT 0,50');
 sql.Open;
 resultados.RowCount := sql.RecordCount+1;
 resultados.ColCount := sql.fields.Count;
 for i := 0 to sql.fields.Count -1 do resultados.Cells[i,0] := sql.fields[i].FieldName;
  if sql.RecordCount > 0 then begin
   for i := 1 to sql.RecordCount do begin
    for i2 := 0 to sql.Fields.Count -1 do resultados.Cells[i2,i] := sql.fields[i2].asString;
    sql.Next;
   end;
  end;
 sql.Close;
 autosizegrid(resultados);
end;

procedure TfrmRelatorio.bt_proximaClick(Sender: TObject);
var posicao,index,qtd:word;
begin
 posicao := pos('LIMIT',sql.SQL.Text);
 if posicao > 0 then begin
  index := strtoint(trim(frmPrincipal.split(copy(sql.SQL.Text,posicao+5,length(sql.SQL.text)-(posicao+5)),',',1)));
  qtd := strtoint(trim(frmPrincipal.split(copy(sql.SQL.Text,posicao+5,length(sql.SQL.text)-(posicao+5)),',',2)));
  sql.sql.Delete(sql.sql.Count-1);
  sql.SQL.add('LIMIT '+inttostr(index+qtd)+','+inttostr(qtd));
 end else sql.SQL.Add('LIMIT 0,50');
 carrega_relatorio;
end;

procedure TfrmRelatorio.resultadosDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  TempPString:Array [0..255] of char;
begin
If (gdFixed in State){or (gdSelected in State)} then exit;
resultados.Canvas.Brush.Style:=bsSolid;
if odd(aRow) then resultados.Canvas.Brush.Color:=clSilver;
resultados.Canvas.FillRect(Rect);
StrPCopy(TempPString,resultados.Cells[ACol,ARow]);
DrawText(resultados.Canvas.Handle,TempPString,-1,Rect,DT_LEFT);
end;

procedure TfrmRelatorio.bt_anteriorClick(Sender: TObject);
var posicao,index,qtd:word;
begin
 posicao := pos('LIMIT',sql.SQL.Text);
 if posicao > 0 then begin
  index := strtoint(trim(frmPrincipal.split(copy(sql.SQL.Text,posicao+5,length(sql.SQL.text)-(posicao+5)),',',1)));
  qtd := strtoint(trim(frmPrincipal.split(copy(sql.SQL.Text,posicao+5,length(sql.SQL.text)-(posicao+5)),',',2)));
  sql.sql.Delete(sql.sql.Count-1);
  sql.SQL.add('LIMIT '+inttostr(index-qtd)+','+inttostr(qtd));
 end else sql.SQL.Add('LIMIT 0,50');
 carrega_relatorio;
end;

end.
